package edu.fdzc.learn.service.impl;

import edu.fdzc.learn.entity.Course;
import edu.fdzc.learn.entity.Lesson;
import edu.fdzc.learn.entity.User;
import edu.fdzc.learn.entity.vo.CouTeaLesVo;
import edu.fdzc.learn.mapper.CourseMapper;
import edu.fdzc.learn.mapper.LessonsMapper;
import edu.fdzc.learn.mapper.UserMapper;
import edu.fdzc.learn.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LessonsMapper lessonsMapper;

    @Override
    public void insert(Course course) {
        course.setCreatedAt(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public List<Course> findall() {
        return courseMapper.findall();
    }

    @Override
    public Course findById(int id) {
        return courseMapper.findById(id);
    }

    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }

    @Override
    public void deleteById(int id) {
        courseMapper.deleteByid(id);
    }

    @Override
    public void deleteByIds(int[] ids) {
        for(int id : ids){
            courseMapper.deleteByid(id);
        }
    }

    @Override
    public CouTeaLesVo findAllTeaLes(int courseId) {
        CouTeaLesVo vo = new CouTeaLesVo();
        Course course = courseMapper.findById(courseId);
        log.info("course:{}",course);
        BeanUtils.copyProperties(course,vo);
        User u = userMapper.findById(course.getTeacherId());
        log.info("u:{}",u);
        vo.setUser(u);
        List<Lesson> list = lessonsMapper.fingByCourseId(courseId);
        vo.setLessons(list);
        return vo;
    }
}
